package com.junci.mtest.views.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.junci.mtest.R;


import com.junci.mtest.databinding.FragmentPlanBinding;
import com.junci.mtest.model.bean.TestPlanBean;
import com.junci.mtest.views.BaseFragment;
import com.junci.mtest.utils.NavigationUtil;
import com.junci.mtest.views.activity.PlanActivity;
import com.junci.mtest.views.adapter.PlanAdapter;
import com.junci.mtest.vm.PlanViewModel;

import java.util.List;


/**
 * Created by YM
 * Create Date 2024/5/1 0:46
 * Used
 */
public class PlanFragment extends BaseFragment {
    private static PlanFragment f;

    private View view;
    public static PlanFragment newInstance() {
        if (f == null) {
            f = new PlanFragment();
        }
        return f;
    }
    FragmentPlanBinding binding;
    PlanViewModel viewModel;
    PlanAdapter planAdapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            binding =  DataBindingUtil.inflate(inflater, R.layout.fragment_plan, container, false);
            view = binding.getRoot();
        }
        viewModel = new ViewModelProvider(this).get(PlanViewModel.class);
        initView();
        bindOb();
        viewModel.fetchItems();
        return view;
    }

    private void initView() {
        binding.xRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                viewModel.fetchItems();
            }
        });
        binding.rlvPlan.setLayoutManager(new GridLayoutManager(getActivity(),2));
        planAdapter = new PlanAdapter(getActivity(),viewModel);
        binding.rlvPlan.setAdapter(planAdapter);
        DividerItemDecoration xdivider = new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL);
        xdivider.setDrawable(getResources().getDrawable(R.drawable.line_1));
        binding.rlvPlan.addItemDecoration(xdivider);
    }

    @SuppressLint({"FragmentLiveDataObserve", "UseCompatLoadingForDrawables"})
    private void bindOb() {
        viewModel.getItems().observe(this, this::refresh);
        viewModel.getItem().observe(this, rows -> {
            NavigationUtil navigationUtil =  NavigationUtil.build(getActivity(), PlanActivity.class);
            navigationUtil.getIntent().putExtra("data",rows);
            navigationUtil.start();
        });
    }

    private void refresh(List<TestPlanBean.Data.Rows> rows){
        binding.xRefresh.setRefreshing(false);
        planAdapter.setItems(rows);
    }
    




}
